﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
using BUS;
using DTO;
namespace GUI
{
    public partial class FormXemChiTietHoaDom : Window
    {
        public int MaHD;
        public FormXemChiTietHoaDom()
        {
            InitializeComponent();
            hoiThoat = false;
        }
        public FormXemChiTietHoaDom(int MaHD_)
        {
            InitializeComponent();
            MaHD = MaHD_;
            lbMaHD.Text = MaHD_.ToString();
            HoaDonBUS hdbus = new HoaDonBUS();
            if (hdbus.IsExport(MaHD) == 1)
            {
                btnThem.Enabled = false;
                btnXoa.Enabled = false;
                btnCapNhat.Enabled = false;
                txtSLMoi.Visible = false;
                label2.Visible = false;
            }

        }
        private void FormXemChiTietHoaDom_Load(object sender, EventArgs e)
        {
            System.Data.OleDb.OleDbDataAdapter da = BUS.CTHoaDonBUS.LayTatCaCTHoaDon(int.Parse(lbMaHD.Text));
            System.Data.DataSet ds = new DataSet();
            da.Fill(ds, "CTHoaDon");
            dataGridView1.DataSource = ds.Tables["CTHoaDon"];
            dataGridView1.Columns[0].Visible = false; // MaHD
            dataGridView1.Columns[2].Visible = false;
        }
        private void btnThem_Click(object sender, EventArgs e)
        {
            SubFormThemCTHoaDon frm = new SubFormThemCTHoaDon(int.Parse(lbMaHD.Text));
            frm.MdiParent = this.MdiParent;
            frm.Show();
            FormXemChiTietHoaDom_Load(null, null);
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            if (dataGridView1.RowCount == 0)
                return;
            DTO.CTHoaDonDTO cthdDTO = new CTHoaDonDTO();
            DataGridViewRow row = dataGridView1.CurrentRow;
            cthdDTO.MaHD = int.Parse(row.Cells[0].Value.ToString());
            cthdDTO.MaSP = int.Parse(row.Cells[1].Value.ToString());
            cthdDTO.SoHD = "0";
            cthdDTO.TenSP = "";
            cthdDTO.SoLuong = int.Parse(row.Cells[4].Value.ToString());
            cthdDTO.DonGia = 0;
            cthdDTO.ThanhTien = double.Parse(row.Cells[6].Value.ToString());
            cthdDTO.DiemCV = int.Parse(row.Cells[7].Value.ToString());
            CTHoaDonBUS cthdbus = new CTHoaDonBUS();
            cthdbus.XoaCTHD(cthdDTO);
            FormXemChiTietHoaDom_Load(null, null);
        }

        private void btnCapNhat_Click(object sender, EventArgs e)
        {
            if (txtSLMoi.ReadOnly == true)
            {
                txtSLMoi.ReadOnly = false;
                return;
            }
            if (!KiemTraSo(txtSLMoi.Text.Trim()))
            {
                MessageBox.Show("");
                txtSLMoi.Text = "";
                txtSLMoi.Focus();
                return;
            }
            if (txtSLMoi.Text.Trim() == "")
            {
                MessageBox.Show("");
                txtSLMoi.Text = "";
                txtSLMoi.Focus();
                return;
            }
            int newSoLuong = int.Parse(txtSLMoi.Text.Trim());
            BUS.CTHoaDonBUS cthdBUS = new CTHoaDonBUS();
            DTO.CTHoaDonDTO cthdDTO = new CTHoaDonDTO();
            #region Clone CTHoaDon
            DataGridViewRow row = dataGridView1.CurrentRow;
            cthdDTO.MaHD = int.Parse(row.Cells[0].Value.ToString());
            cthdDTO.MaSP = int.Parse(row.Cells[1].Value.ToString());
            cthdDTO.SoHD = "0";
            cthdDTO.TenSP = row.Cells[3].Value.ToString();
            cthdDTO.SoLuong = int.Parse(row.Cells[4].Value.ToString());
            cthdDTO.DonGia = double.Parse(row.Cells[5].Value.ToString());
            cthdDTO.ThanhTien = double.Parse(row.Cells[6].Value.ToString());
            cthdDTO.DiemCV = int.Parse(row.Cells[7].Value.ToString());
            #endregion
            BUS.SanPhamBUS spBus = new SanPhamBUS();
            DTO.SanPhamDTO spDTO = spBus.LayTTSP(cthdDTO.MaSP);
            DTO.CTHoaDonDTO cthdDTO_ = new CTHoaDonDTO(cthdDTO);
            cthdDTO_.SoLuong = newSoLuong;
            cthdDTO_.ThanhTien = cthdDTO_.SoLuong * cthdDTO_.DonGia;

            cthdDTO_.DiemCV = cthdDTO_.SoLuong * spDTO.DiemCV;
            cthdBUS.XoaCTHD(cthdDTO);
            cthdBUS.themCTvaoHDDaTonTai(cthdDTO_);
            FormXemChiTietHoaDom_Load(null, null);
            txtSLMoi.Text = "";
            txtSLMoi.ReadOnly = true;
        }
        private void button1_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            //HoaDonBUS bus = new HoaDonBUS();
            //DTO.HoaDonDTO hdDTO = new HoaDonDTO();
            //int MaHD_DuocChon = this.MaHD;
            //hdDTO = bus.LayTTHD(MaHD_DuocChon);
            //if (hdDTO.ThanhTien == 0)
            //    bus.HuyHoaDon(MaHD_DuocChon);
            this.Close();
        }

        private void FormXemChiTietHoaDom_FormClosed(object sender, FormClosedEventArgs e)
        {
            BUS.HoaDonBUS bus = new HoaDonBUS();
            bus.XoaHoaDonThanhTienbang0();
        }
    }
}
